//注册
const db = require("../lib/db")

function zhuce(username,userpwd,desc,headImg){
    return new Promise((resolve , reject) =>{
        let sql = "select * from user where userName = '" + username + "'";
        console.log(sql)
        db.querySql(sql).then( data => {
            if(data.length > 0){
                // 用户已存在
                resolve(501)
                console.log("已经注册过了")
            }else{
                let sql = `insert into user(userName,userPassword,desc1,headImg)values('${username}','${userpwd}','${desc}','${headImg}')`;
                console.log(sql)
                db.querySql(sql).then(data => {
                    if(data.length == 0) {
                        // 注册失败
                        resolve(502)
                    }else{
                        // 注册成功
                        resolve(200)
                    }        
                })
            }          
        })        
    })
}
module.exports = zhuce;